package com.kdgc.power.common.util;


import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ReflectUtil {
    public static void reflectClass(String className) {
        Class<?> aClass;
        try {
            aClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            System.out.println("未找到类：" + className);
            return;
        }

        //类的头部
        String stringBuffer = "" + Modifier.toString(aClass.getModifiers()) +
                " class " +
                aClass.getSimpleName() + "{";
        System.out.println(stringBuffer);

        //类的成员属性
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.print("\t" + Modifier.toString(declaredField.getModifiers()) + " ");
            System.out.print(declaredField.getType().getSimpleName() + " ");
            System.out.println(declaredField.getName() + ";");
        }
        System.out.println();

        //类的构造方法
        Constructor<?>[] constructors = aClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.print("\t" + Modifier.toString(constructor.getModifiers()) + " ");
            System.out.print(aClass.getSimpleName() + "(");
            //构造方法的参数列表
            Parameter[] parameters = constructor.getParameters();
            boolean isFirst = true;
            for (Parameter parameter : parameters) {
                if (isFirst) {
                    System.out.print(parameter.getType().getSimpleName() + " ");
                    System.out.print(parameter.getName());
                    isFirst = false;
                } else {
                    System.out.print(", " + parameter.getType().getSimpleName() + " ");
                    System.out.print(parameter.getName());
                }
            }
            System.out.println("){   }");
        }
        System.out.println();

        //类的普通方法
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            System.out.print("\t" + Modifier.toString(method.getModifiers()) + " ");
            System.out.print(method.getReturnType().getSimpleName() + " ");
            System.out.print(method.getName() + "(");
            //普通方法的参数列表
            Parameter[] parameters = method.getParameters();
            boolean isFirst = true;
            for (Parameter parameter : parameters) {
                if (isFirst) {
                    System.out.print(parameter.getType().getSimpleName() + " ");
                    System.out.print(parameter.getName());
                    isFirst = false;
                } else {
                    System.out.print(", " + parameter.getType().getSimpleName() + " ");
                    System.out.print(parameter.getName());
                }
            }
            System.out.println("){   }");
        }
        System.out.println("}");
        System.out.println();

    }

    public static void bornTableSql(String className) {
        Class<?> aClass;
        try {
            aClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            System.out.println("未找到类：" + className);
            return;
        }
        String tableName = StringUtil.toUnderline(aClass.getSimpleName()).toUpperCase(Locale.ROOT);
        Field[] declaredFields = aClass.getDeclaredFields();
        List<String> name = new ArrayList<>();
        List<String> type = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            String columnName = StringUtil.toUnderline(declaredField.getName()).toUpperCase(Locale.ROOT);
            name.add(columnName);
            String dataType = "";
            switch (declaredField.getType().getSimpleName()) {
                case "Integer":
                    dataType = "NUMBER(10)";
                    break;
                case "Date":
                    dataType = "TIMESTAMP(6)";
                    break;
                default:
                    dataType = "VARCHAR(32)";
            }
            type.add(dataType);
        }
        StringBuilder builder = new StringBuilder();
        builder.append("create table ");
        builder.append(tableName);
        builder.append(" ( ");
        for (int i = 0; i < name.size(); i++) {
            if (i == 0) {
                builder.append(name.get(0));
                builder.append(" ");
                builder.append(type.get(0));
                builder.append("PRIMARY KEY ,");
            } else if (i < name.size() - 1) {
                builder.append(name.get(i));
                builder.append(" ");
                builder.append(type.get(i));
                builder.append(",");
            } else {
                builder.append(name.get(i));
                builder.append(" ");
                builder.append(type.get(i));
                builder.append(");");
            }
        }
        for (String s : name) {
            builder.append("comment on column ");
            builder.append(tableName);
            builder.append(".");
            builder.append(s);
            builder.append(" is '';");
        }
        System.out.println(builder);
    }


}

